<?php

namespace api\modules\mini\controllers;

use api\controllers\OnAuthController;
use common\helpers\CommonHelper;
use common\models\spoker\Spoker;
use services\linkedcare\LinkedcarePatientsFeeDetailService;
use services\linkedcare\LinkedcarePatientsRecordService;
use services\patient\PatientService;
use Yii;

class RankController extends OnAuthController
{
    public $modelClass = '';

    public function beforeAction($action)
    {
        $res = parent::beforeAction($action); // TODO: Change the autogenerated stub
        $user = Yii::$app->user->identity->user;
        if ($user && $user->spoker && $user->spoker->status != Spoker::STATUS_SUCCESS) {
            CommonHelper::api(422, '代言人未审核通过，无权限查看');
            return false;
        }
        return $res;
    }

    /**
     * @api {get} /mini/rank/patient-list  新增排行列表
     * @apiName patient-list
     * @apiGroup MiniRank
     * @apiUse CommonParam
     *
     * @apiParam {String} start_time        开始时间【格式：YYYY-MM-DD】
     * @apiParam {String} end_time         结束时间【格式：YYYY-MM-DD】
     *
     * @apiSuccess {Array}   data               返回数据
     * @apiSuccess {Number}  data.spoker_id     ID
     * @apiSuccess {Number}  data.arrive_num    到店数
     * @apiSuccess {Number}  data.name          推荐人
     */
    public function actionPatientList()
    {
        $params = Yii::$app->request->get();

        $service = new PatientService();
        return $service->getRankList($params);
    }

    /**
     * @api {get} /mini/rank/arrive-list  到店排行列表
     * @apiName arrive-list
     * @apiGroup MiniRank
     * @apiUse CommonParam
     *
     * @apiParam {String} start_time        开始时间【格式：YYYY-MM-DD】
     * @apiParam {String} end_time         结束时间【格式：YYYY-MM-DD】
     *
     * @apiSuccess {Array}   data               返回数据
     * @apiSuccess {Number}  data.spoker_id     ID
     * @apiSuccess {Number}  data.arrive_num    到店数
     * @apiSuccess {Number}  data.name          推荐人
     */
    public function actionArriveList()
    {
        $params = Yii::$app->request->get();

        $service = new LinkedcarePatientsRecordService();
        return $service->getRankList($params);
    }

    /**
     * @api {get} /mini/rank/consume-list  消费排行列表
     * @apiName consume-list
     * @apiGroup MiniRank
     * @apiUse CommonParam
     *
     * @apiParam {String} start_time        开始时间【格式：YYYY-MM-DD】
     * @apiParam {String} end_time         结束时间【格式：YYYY-MM-DD】
     *
     * @apiSuccess {Array}   data               返回数据
     * @apiSuccess {Number}  data.spoker_id     ID
     * @apiSuccess {Number}  data.arrive_num    到店数
     * @apiSuccess {Number}  data.name          推荐人
     */
    public function actionConsumeList()
    {
        $params = Yii::$app->request->get();

        $service = new LinkedcarePatientsFeeDetailService();
        return $service->getRankList($params);
    }
}
